<template>
  <div class="createPost-container">
    <el-form v-if="!infoLoading" ref="postForm" :model="postForm" :rules="rules" class="form-container">
      <div class="createPost-main-container">
        <el-row>
          <el-col :span="24">

            <div class="postInfo-container">

              <el-form-item label-width="100px" prop="jyyj" label="检验依据">
                <el-input v-model="postForm.jyyj" />
              </el-form-item>
              <el-form-item label-width="100px" prop="jyxm" label="检验项目">
                <el-input v-model="postForm.jyxm" />
              </el-form-item>
              <el-form-item label-width="100px" prop="jyjl" label="检验结论汇总">
                <el-input v-model="postForm.jyjl" />
              </el-form-item>
            </div>

          </el-col>
        </el-row>

        <el-form-item v-if="!readOnly" style="margin-bottom: 30px;">
          <el-button type="primary" :loading="loading" @click="submitForm">提交</el-button>
        </el-form-item>

      </div>
    </el-form>
  </div>
</template>

<script>

const defaultForm = {
  jyyj: '',
  jyxm: '',
  jyjl: '', // 描述
  id: null
}

export default {
  name: 'ArticleDetail',
  props: {
    readOnly: {
      type: Boolean,
      default: false
    },
    infoid: {
      type: Number,
      default: 0
    },
    timestamp: {
      type: Number,
      default: 0
    }
  },
  data() {
    const validateRequire = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('必填项'))
      } else {
        callback()
      }
    }
    return {
      myHeaders: {},
      infoLoading: false,
      postForm: Object.assign({}, defaultForm),
      loading: false,
      rules: {
        jyjl: [{ validator: validateRequire }],
        jyxm: [{ validator: validateRequire }],
        jyyj: [{ validator: validateRequire }]
      }
    }
  },
  watch: {
    timestamp: {
      handler(val, oldVal) {
        console.log('timestamp:' + val)
        this.getData(this.infoid)
      },
      immediate: true
    }
  },
  mounted() {
    this.myHeaders = {
      Authorization: 'Bearer ' + localStorage.getItem('token')
    }
  },
  methods: {
    getData(id) {
      this.infoLoading = true
      if (id) {
        this.$http
          .get('/api/jyjl/getById?&id=' + id)
          .then(response => {
            this.infoLoading = false
            if (response.data.success === true) {
              this.postForm = response.data.result
            } else {
              console.log('获取失败:' + response.data.message)
              this.$message({
                message: '获取失败:' + response.data.message,
                type: 'error'
              })
            }
          })
          .catch(error => {
            console.log(error)
            this.infoLoading = false
          })
      } else {
        this.$nextTick(() => {
          this.infoLoading = false
          this.postForm = Object.assign({}, defaultForm)
        })
      }
    },
    submitForm() {
      this.$refs.postForm.validate(valid => {
        if (valid) {
          this.saveInfo()
        }
      })
    },
    saveInfo() {
      const para = {
        jyjl: this.postForm.jyjl,
        jyxm: this.postForm.jyxm,
        jyyj: this.postForm.jyyj,
        id: this.postForm.id ? this.postForm.id : null
      }
      this.loading = true
      this.$http
        .post('/api/jyjl/save', para)
        .then(response => {
          this.loading = false
          this.postForm.status = 'published'
          this.postForm = Object.assign({}, defaultForm)
          if (response.data.success === true) {
            this.$message({
              message: '提交成功',
              type: 'success'
            })
            this.$emit('dialogHide')
          } else {
            console.log('提交失败:' + response.data.message)
            this.$message({
              message: '提交失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(function(error) {
          console.log(error)
          this.loading = false
        })
    }
  }
}
</script>

<style lang="scss" scoped>
@import "~@/styles/mixin.scss";

.createPost-container {
  position: relative;

  .createPost-main-container {
    // padding: 40px 45px 20px 50px;

    .postInfo-container {
      position: relative;
      @include clearfix;
      margin-bottom: 10px;

      .postInfo-container-item {
        float: left;
      }
    }
  }

  .word-counter {
    width: 40px;
    position: absolute;
    right: 10px;
    top: 0px;
  }
}

.article-textarea ::v-deep {
  textarea {
    padding-right: 40px;
    resize: none;
    border: none;
    border-radius: 0px;
    border-bottom: 1px solid #bfcbd9;
  }
}
</style>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
